package com.zys.http.ui.tab.panel;

import com.intellij.openapi.fileTypes.FileType;
import com.zys.http.constant.HttpEnum;
import com.zys.http.entity.ReqHistory;
import com.zys.http.entity.param.ParamProperty;
import com.zys.http.ui.editor.CustomEditor;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.awt.*;
import java.awt.event.ItemListener;
import java.util.Map;
import java.util.Objects;

/**
 * @author zys
 * @since 2025-02-19
 */
public interface TabOperation {

    void reset();

    void execute(HttpEnum.HttpMethod httpMethod, HttpEnum.ContentType contentType, String param, Map<String, ParamProperty> paramPropertyMap);

    void recover(ReqHistory history);

    @Contract(pure = true)
    default @NotNull ItemListener itemListener(CustomEditor customEditor) {
        return e -> {
            ItemSelectable item = e.getItemSelectable();
            if (Objects.isNull(item)) {
                return;
            }
            Object[] selects = item.getSelectedObjects();
            if (Objects.isNull(selects) || selects.length < 1) {
                return;
            }
            Object select = selects[0];
            if (select instanceof FileType fileType) {
                customEditor.setFileType(fileType);
            }
        };
    }
}
